﻿using Castle.Facilities.FactorySupport;
using Castle.MicroKernel.Registration;
using Castle.Windsor;
using NHibernate;
using NHibernate.Engine;
using NHibernate.Tool.hbm2ddl;
using uNhAddIns.CastleAdapters;
using uNhAddIns.CastleAdapters.AutomaticConversationManagement;
using uNhAddIns.CastleAdapters.EnhancedBytecodeProvider;
using uNhAddIns.NHibernateTypeResolver;
using uNhAddIns.SessionEasier;
using uNhAddIns.SessionEasier.Conversations;
using uNhAddIns.WPF.Collections;
using Environment = NHibernate.Cfg.Environment;
using System;

namespace XWord.GuyWire.Configurators
{
    public class NHibernateConfigurator : IConfigurator
    {
        #region IConfigurator Members

        public void Configure(IWindsorContainer container)
        {
            container.AddFacility<PersistenceConversationFacility>();
            container.AddFacility<FactorySupportFacility>();

            Environment.BytecodeProvider = new EnhancedBytecode(container);

            var nhConfigurator = new DefaultSessionFactoryConfigurationProvider();

            nhConfigurator.BeforeConfigure += (sender, e) =>
            {
                e.Configuration.RegisterEntityNameResolver();
                e.Configuration.Properties[Environment.CollectionTypeFactoryClass]
                    = typeof(WpfCollectionTypeFactory).AssemblyQualifiedName;
            };

            nhConfigurator.AfterConfigure += (sender, e) =>
            {
                e.Configuration.Properties[Environment.ConnectionString] = string.Format(e.Configuration.Properties[Environment.ConnectionString], System.Environment.CurrentDirectory + "\\");
            };

            var sfp = new SessionFactoryProvider(nhConfigurator);

            //container.Register(Component.For<IBehaviorStore>().ImplementedBy<BehaviorDictionary>());

            container.Register(Component.For<ISessionFactoryProvider>().Instance(sfp));
            container.Register(Component.For<ISessionFactory>().UsingFactoryMethod(
                    () => container.Resolve<ISessionFactoryProvider>().GetFactory(null)));

            container.Register(Component.For<ISessionFactoryImplementor>().UsingFactoryMethod(
                    () => (ISessionFactoryImplementor)container.Resolve<ISessionFactoryProvider>().GetFactory(null)));

            container.Register(Component.For<ISessionWrapper>().ImplementedBy<SessionWrapper>());
            container.Register(Component.For<IConversationFactory>().ImplementedBy<DefaultConversationFactory>());
            container.Register(Component.For<IConversationsContainerAccessor>().ImplementedBy<NhConversationsContainerAccessor>());
        }

        #endregion
    }
}